package com.example.a_java.controller;

import com.example.a_java.common.Result;
import com.example.a_java.service.DbInitService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.security.SecurityRequirement;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

/**
 * 数据库初始化控制器
 */
@Tag(name = "数据库初始化", description = "数据库初始化相关接口")
@RestController
@RequestMapping("/system/db")
@SecurityRequirement(name = "bearer-key")
public class DbInitController {

    @Autowired
    private DbInitService dbInitService;

    /**
     * 检查数据库表是否存在
     */
    @Operation(summary = "检查数据库表是否存在")
    @GetMapping("/check-tables")
    @PreAuthorize("hasAuthority('system:db:admin')")
    public Result<Map<String, Boolean>> checkTables() {
        Map<String, Boolean> tableStatus = dbInitService.checkTablesExist();
        return Result.success(tableStatus);
    }

    /**
     * 初始化数据库表
     */
    @Operation(summary = "初始化数据库表")
    @PostMapping("/init-tables")
    @PreAuthorize("hasAuthority('system:db:admin')")
    public Result<Map<String, Object>> initTables() {
        int tablesCreated = dbInitService.initTables();
        
        Map<String, Object> result = new HashMap<>();
        result.put("tablesCreated", tablesCreated);
        result.put("success", tablesCreated > 0);
        
        return Result.success(result);
    }

    /**
     * 初始化示例数据
     */
    @Operation(summary = "初始化示例数据")
    @PostMapping("/init-sample-data")
    @PreAuthorize("hasAuthority('system:db:admin')")
    public Result<Map<String, Object>> initSampleData() {
        int recordsInserted = dbInitService.initSampleData();
        
        Map<String, Object> result = new HashMap<>();
        result.put("recordsInserted", recordsInserted);
        result.put("success", recordsInserted > 0);
        
        return Result.success(result);
    }

    /**
     * 执行完整初始化
     */
    @Operation(summary = "执行完整初始化")
    @PostMapping("/init-all")
    @PreAuthorize("hasAuthority('system:db:admin')")
    public Result<Map<String, Object>> initAll() {
        Map<String, Object> result = dbInitService.initAll();
        return Result.success(result);
    }
    
    /**
     * 检查统计表是否存在
     */
    @Operation(summary = "检查统计表是否存在")
    @GetMapping("/check-stats-tables")
    @PreAuthorize("hasAuthority('system:db:admin')")
    public Result<Map<String, Boolean>> checkStatsTables() {
        Map<String, Boolean> tableStatus = dbInitService.checkStatsTablesExist();
        return Result.success(tableStatus);
    }
    
    /**
     * 初始化统计表
     */
    @Operation(summary = "初始化统计表")
    @PostMapping("/init-stats-tables")
    @PreAuthorize("hasAuthority('system:db:admin')")
    public Result<Map<String, Object>> initStatsTables() {
        int tablesCreated = dbInitService.initStatsTables();
        
        Map<String, Object> result = new HashMap<>();
        result.put("tablesCreated", tablesCreated);
        result.put("success", tablesCreated > 0);
        
        return Result.success(result);
    }
    
    /**
     * 初始化统计示例数据
     */
    @Operation(summary = "初始化统计示例数据")
    @PostMapping("/init-stats-sample-data")
    @PreAuthorize("hasAuthority('system:db:admin')")
    public Result<Map<String, Object>> initStatsSampleData() {
        int recordsInserted = dbInitService.initStatsSampleData();
        
        Map<String, Object> result = new HashMap<>();
        result.put("recordsInserted", recordsInserted);
        result.put("success", true); // 即使没有数据插入也视为成功
        
        return Result.success(result);
    }
    
    /**
     * 执行统计完整初始化
     */
    @Operation(summary = "执行统计完整初始化")
    @PostMapping("/init-stats-all")
    @PreAuthorize("hasAuthority('system:db:admin')")
    public Result<Map<String, Object>> initStatsAll() {
        Map<String, Object> result = dbInitService.initStatsAll();
        return Result.success(result);
    }
} 
